<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="编号">
              <a-input placeholder="" v-model="queryParam.clientId"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="密钥">
              <a-input placeholder="" v-model="queryParam.clientSecret"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button @click="() => {this.queryPage()}" type="primary">查询</a-button>
            <a-button @click="() => queryParam = {}" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <v-table
      bordered
      ref="table"
      :data="loadData"
      :columns="columns"
    >
      <template v-slot:buttons>
        <a-button @click="$refs.clientAddOrUpdate.init('','add')" icon="plus" type="primary" v-action="'sys:client:save'">新建</a-button>
      </template>
      <span slot="autoapprove" slot-scope="{row,text}">
        {{text ? '是' : '否'}}
      </span>
      <span slot="action" slot-scope="{row,text}">
        <span v-action="'sys:client:update'">
          <a @click="edit(row)">修改</a>
          <a-divider type="vertical" />
        </span>
        <span v-action="'sys:client:del'">
          <a-popconfirm title="是否删除字典" @confirm="del(row)" okText="是" cancelText="否">
            <a-icon slot="icon" type="question-circle-o" style="color: red" />
            <a href="javascript:;" style="color: red">删除</a>
            <a-divider type="vertical" />
          </a-popconfirm>
        </span>
        <span v-action="'sys:client:info'">
          <a @click="show(row)">查看</a>
        </span>
      </span>
    </v-table>
    <client-add-or-update
      @ok="queryPage"
      ref="clientAddOrUpdate"
    ></client-add-or-update>
  </a-card>
</template>

<script>
import { fetchList, delObj } from '@/api/sys/client'
import ClientAddOrUpdate from './clientAddOrUpdate'
import { TableMixin } from '@/mixins/TableMixin'
export default {
  name: 'Client',
  mixins: [TableMixin],
  components: {
    ClientAddOrUpdate
  },
  data () {
    return {
      columns: [
        {
          title: '编号',
          field: 'clientId'
        },
        {
          title: '密钥',
          field: 'clientSecret'
        },
        {
          title: '域',
          field: 'scope'
        },
        {
          title: '授权方式',
          field: 'authorizedGrantTypes'
        },
        {
          title: '令牌时效',
          field: 'accessTokenValidity'
        },
        {
          title: '刷新时效',
          field: 'refreshTokenValidity'
        },
        {
          title: '自动放行',
          field: 'autoapprove',
          slot: true
        },
        {
          title: '操作',
          field: 'action',
          slot: true
        }
      ],
      queryParam: {
        clientId: '',
        clientSecret: ''
      },
      loadData: parameter => {
        return fetchList(
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    edit (record) {
      this.$refs.clientAddOrUpdate.init(record.clientId, 'edit')
    },
    del (record) {
      delObj(record.clientId).then(res => {
        this.$message.info('删除成功')
        this.queryPage()
      })
    },
    show (record) {
      this.$refs.clientAddOrUpdate.init(record.clientId, 'show')
    }
  }
}
</script>

<style scoped>

</style>
